const {exec} = require('../db/mysql')

const getType = async ()=>{
    const sql = `
        select typeId,unitName,typeName,unitSymbol from bodyType;
    `
    const rows = await exec(sql)
    return rows
}

const recordBody = async(blogData={})=>{
    const {typeId,bodyData,userId}=blogData
    const createDate=Date.now()
    const sql = `
    insert into bodys(typeId,bodyData,userId,createDate)
    values ('${typeId}',${bodyData},${userId},'${createDate}');
    `
    const insertData = await exec(sql);
    return  {
        id:insertData.id
    }
}
const getBody = async(blogData={})=>{
    const {typeId,userId} = blogData
    const sql = `select * from bodys join bodyType 
    on bodys.typeId = bodyType.typeId 
    where bodys.userid = ${userId} and bodys.typeId = ${typeId}`
    const rows = await exec(sql)
    return rows||[]
}
module.exports={
    getType,
    recordBody,
    getBody
}